﻿/********************************************************
 * ADO.NET 2.0 Data Provider for SQLite Version 3.X
 * Written by Robert Simpson (robert@blackcastlesoft.com)
 * 
 * Released to the public domain, use at your own risk!
 ********************************************************/
using System;
using Microsoft.Win32;
using Microsoft.VisualStudio.Data;
using Microsoft.VisualStudio.Data.AdoDotNet;
using Microsoft.VisualStudio.Shell.Interop;


namespace EffiProz.Designer
{
    class EfzConnectionProperties : AdoDotNetConnectionProperties
    {
        public EfzConnectionProperties()
            : this(null)
        {
        }

        public EfzConnectionProperties(string connectionString)
            : base("System.Data.EffiProz", connectionString)
        {
        }

        public override string[] GetBasicProperties()
        {
            return new string[] { "connection type", "initial catalog" };
        }

        protected override bool ShouldPersistProperty(string propertyName)
        {
            if (String.Compare(propertyName, "database", StringComparison.OrdinalIgnoreCase) == 0) return false;
            else if (String.Compare(propertyName, "password", StringComparison.OrdinalIgnoreCase) == 0) return true;
            else if (String.Compare(propertyName, "user", StringComparison.OrdinalIgnoreCase) == 0) return true;
            else if (String.Compare(propertyName, "inital catalog", StringComparison.OrdinalIgnoreCase) == 0) return true;
            else if (String.Compare(propertyName, "data source", StringComparison.OrdinalIgnoreCase) == 0) return true;
            else if (String.Compare(propertyName, "type", StringComparison.OrdinalIgnoreCase) == 0) return true;
            else if (String.Compare(propertyName, "host", StringComparison.OrdinalIgnoreCase) == 0) return true;
            else if (String.Compare(propertyName, "port", StringComparison.OrdinalIgnoreCase) == 0) return true;

            return base.ShouldPersistProperty(propertyName);
        }

        public override bool Contains(string propertyName)
        {
            if (String.Compare(propertyName, "Database", StringComparison.OrdinalIgnoreCase) == 0)
                return (base.Contains("initial catalog") || base.Contains("uri"));

            if (String.Compare(propertyName, "data source", StringComparison.OrdinalIgnoreCase) == 0)
                return (base.Contains("host") || base.Contains("port"));

            return base.Contains(propertyName);
        }

        public override object this[string propertyName]
        {
            get
            {
                if (String.Compare(propertyName, "Database", StringComparison.OrdinalIgnoreCase) == 0)
                    return (string)base["initial catalog"];
                if (String.Compare(propertyName, "data source", StringComparison.OrdinalIgnoreCase) == 0)
                    return (string)base["host"]+":"+(string)base["port"];

                return base[propertyName];
            }
            set
            {
                base[propertyName] = value;
            }
        }

        internal string GetDatabaseFile()
        {
            if (this["initial catalog"] is string && ((string)this["initial catalog"]).Length > 0)
                return (string)this["initial catalog"];

            return String.Empty;
        }

        private bool IsValidType(string type)
        {
            if (type.Equals("file", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            else if (type.Equals("server", StringComparison.OrdinalIgnoreCase))
            {
                if (Contains("host") && Contains("port"))
                {

                    int dummy;
                    return (this["host"] is string && ((string)this["host"]).Length > 0) &&
                        (this["port"] is int || (this["port"] is string && int.TryParse((string)this["port"], out dummy)));
                }
                else
                    return false;
            }
            else
                return false;
        }

        public override bool IsComplete
        {      

            get
            {

                if (Contains("initial catalog") == true && Contains("user") == true)
                {
                    if ((this["initial catalog"] is string && ((string)this["initial catalog"]).Length > 0) &&
                        (this["connection type"] is string && ((string)this["connection type"]).Length > 0) &&
                        (this["user"] is string && ((string)this["user"]).Length > 0) &&
                         IsValidType((string)this["connection type"]) )                        
                            return true;
                }
                return false;
            }
        }
       
    }
}

